<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>工时查看</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/build/css/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/css/my-style.css">
    <style>        /* 防止下拉框的下拉列表被隐藏---必须设置--- */
    .layui-table-cell {
        overflow: visible !important;
    }

    /* 设置下拉框的高度与表格单元相同 */
    .layui-input, .layui-select, .layui-textarea {
        height: 30px !important;
    }

    .layui-table-tool-self {
        display: none;
    }

    table {
        table-layout: fixed;
    }

    td {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    </style>
</head>
<body>
<div class="kit-doc">
    <!--表格搜索框组-->
    <div class="layui-form-item layui-formgroup layui-tab-card">
        <div class="layui-inline">
            <form class="layui-form " action="">
                <input type="hidden" id="monday">
                <input type="hidden" id="sunday">
                <label class="layui-form-label">项目：</label>
                <div class="layui-input-inline">
                    <select name="interest" id="searchProject" lay-verify="required" lay-search="">
                        <option value=""></option>
                        <option value="" th:each="project : ${projectList}" th:value="${project.getId()}"
                                th:text="${project.getProjectName()}"></option>
                    </select>
                </div>
            </form>
        </div>
        <!-- 时间 -->
        <div class="layui-inline">
            <label class="layui-form-label">日期：</label>
            <div class="layui-input-inline">
                <input class="layui-input" id="test6" placeholder=" 时间段 " type="text">
            </div>
        </div>


        <!-- 按钮 -->
        <button class="layui-btn layui-btn-sm" id="searchBtn">搜索</button>
        <button class="layui-btn layui-btn-sm" id="exportBtn">导出</button>

        <button class="layui-btn layui-btn-primary layui-btn-sm" id="preWeekBtn">
            <i class="fa fa-reply margin-r3"></i>上一周
        </button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="thisWeekBtn">
            <i class="fa fa-search margin-r3"></i>本周
        </button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="nextWeekBtn">
            <i class="fa fa-share margin-r3"></i>下一周
        </button>
    </div>

    <div class="layui-card layui-card-body gs-box">
        <div id="tb" style="height:auto"></div>
        <table class="layui-table xuhao-table " id="table-context" lay-filter="test">
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="review">通过审核</a>
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="goBack">打回重审</a>
<!--                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
            </script>

        </table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn  layui-btn-sm" lay-event="passBatch">批量通过</button>
        <button class="layui-btn  layui-btn-sm" lay-event="flunkBatch">批量打回</button>
<!--        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchDel">批量删除</button>-->
    </div>
</script>

<!--类型id和名字对应的select-->
<!--<select id="projectType" lay-verify="required" lay-search="" style="display:none">-->
<!--    <option value=""></option>-->
<!--    <option value="" th:each="type : ${typeList}" th:value="${type.getId()}" th:text="${type.getTypeName()}"></option>-->
<!--</select>-->

<!--项目id转名字-->
<script type="text/html" id="projectTpl">
    {{ projectToName(d.projectId) }}
</script>

<!--类型id转名字-->
<script type="text/html" id="typeTpl">
    {{ typeToName(d.projectTypeId) }}
</script>

<script src="/js/jquery.min.js"></script>
<script src="/plugins/layui/layui.js"></script>
<script src="/js/workhour.js"></script>
<script>
    var projectToName = function (projectId) {
        return layui.$("#searchProject option[value=" + projectId + "]").text();
    };

    var typeToName = function (typeId) {
        return layui.$("#projectType option[value=" + typeId + "]").text();
    };

</script>
<script>
    var now = new Date();

    // 获取周一和周末
    var start = getTime(0, now);
    $("#monday").val(start);
    var end = getTime(-6, now);
    $("#end").val(end);

    layui.use(['table', 'jquery', 'form'], function () {
// 保存上次查询的数据
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var projectTable = table.render({
            elem: '#table-context'
            , url: '/workhours/admin/view'
            , toolbar: '#toolbarDemo'
            , height: 'full-200'
            , page: true
            , limit: 10
            , limits: [10, 20, 30]
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            },
            where:{
                reviewFlag : "review"
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {
                        field: 'projectId',
                        title: '项目名称',
                        templet: '#projectTpl',
                        align: 'center',
                        width: '15%',
                    },
                    {
                        field: 'username',
                        title: '用户名',
                        align: 'center',
                        width: '10%'
                    },
                    {
                        field: 'remark',
                        title: '任务信息',
                        minWidth: 180,
                        align: 'center'
                    },
                    {
                        field: 'workDate',
                        title: '工作日期',
                        align: 'center',
                        width: '12%',
                    },
                    {
                        title: '工时',
                        field: 'workHours',
                        align: 'center',
                        width: '10%'
                    },
                    {
                        title: '审核状态',
                        // field: 'reviewFlag',
                        width: '10%',
                        align: 'center',
                        templet: function (data) {
                            if(data!=null){
                                var html = "";
                                if(data.reviewFlag == '0'){
                                    html += '<a class=" layui-btn layui-btn-sm  layui-btn-radius layui-btn-primary" lay-event="">未审核</a>'
                                }
                                if(data.reviewFlag == '1'){
                                    html += '<a class=" layui-btn layui-btn-sm  layui-btn-radius layui-btn-normal" lay-event="">审核通过</a>'
                                }
                                if(data.reviewFlag == '2'){
                                    html += '<a class=" layui-btn layui-btn-sm  layui-btn-radius layui-btn-danger" lay-event="">未通过审核</a>'
                                }
                                return html;
                            }
                        }
                    },
                    {
                        title: '操作',
                        fixed: 'right',
                        align: 'center',
                        width: '12%',
                        toolbar: '#barDemo'
                    }
                ]
            ]
            , done: function (res, curr, count) {

            }
        });

// 保存是否被编辑
        var isEdit = false;

        /**
         * 下拉选择项目名称
         */
        form.on('select(projectSelect)', function (obj) {
            var index = $(obj.elem).parent().parent().parent().index();
            var tableData = table.cache["table-context"];
            tableData[index].projectId = obj.value;
            isEdit = true;
        });

        form.on('select(typeSelect)', function (obj) {
            var index = $(obj.elem).parent().parent().parent().index();
            var tableData = table.cache["table-context"];
            tableData[index].projectTypeId = obj.value;
            isEdit = true;
        });


        /**
         * 搜索
         */
        $("#searchBtn").click(function () {
            refresh("1");
        });

        function refresh(reviewFlag) {
            var searchProjectContext = $("#searchProject").val();
            var start = $("#test6").val().split(' - ')[0];
            var end = $("#test6").val().split(' - ')[1];

            projectTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    projectId: searchProjectContext,
                    startDate: start,
                    endDate: end,
                    reviewFlag: reviewFlag
                }
            });
        }



        // 导出
        $("#exportBtn").click(function () {
            var searchProjectContext = $("#searchProject").val();
            var start = $("#test6").val().split(' - ')[0];
            var end = $("#test6").val().split(' - ')[1];
            window.location.href = "/workhours/admin/excel?" +
                "projectId=" + searchProjectContext +
                "&startDate=" + start +
                "&reviewFlag=" + "review" +
                "&endDate=" + end;
        });


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                idList = [];
                domIdList = [];
                switch (obj.event) {
                    case 'passBatch':
                        updateBatch(checkStatus.data,"1");
                        break;
                    case 'flunkBatch':
                        updateBatch(checkStatus.data,"2");
                        break;
                }

            }
        );

        function updateBatch(data,reviewFlag){

            for (var i = 0; i < data.length; i++) {
                data[i].reviewFlag = reviewFlag
            }

            //没有选中任何一行
            if (data.length == 0) {
                layui.layer.msg('请选择行');
                return;
            }
            layui.layer.confirm('确定审核好了吗？', function (index) {
                layui.layer.close(index);
                layer.msg('通过中...', {icon: 16, shade: 0.3, time: 5000});
                layui.$.ajax({
                    type: 'PUT',
                    url: "/workhours/updateBatch",
                    contentType: "application/json",
                    dataType: 'json',
                    data: JSON.stringify(data),
                    success: function (res) {
                        layer.closeAll();
                        if (res.code == 0) {
                            refresh("review");
                            layer.msg("审批成功");

                        } else {
                            layer.msg("审批失败" + data.msg);
                        }
                    }
                });
            });
        }

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'review') {
                layer.confirm('是否通过审核', function (index) {
                    // console.log("审核内容------")
                    updateReviewFlag(data,"1")
                    layer.close(index);
                });
            }
            if (layEvent === 'goBack') {
                layer.confirm('是否退回该审核内容', function (index) {
                    // console.log("退回审核内容------")
                    updateReviewFlag(data,"2")
                    layer.close(index);
                });
            }
        });

        function updateReviewFlag(workHour,reviewFlag) {
            workHour.reviewFlag = reviewFlag
            layui.$.ajax({
                type: 'PUT',
                url: "/workhours/update/"+ workHour.id,
                contentType: "application/json",
                dataType: 'json',
                data: JSON.stringify(workHour),
                success: function (res) {
                    layer.closeAll();
                    if (res.code == 0) {
                        refresh("review");
                        layer.msg("操作成功");
                    } else {
                        layer.msg("操作失败" + data.msg);
                    }
                }
            });
        }

        //监听单元格编辑
        table.on('edit(test)', function (obj) {

            isEdit = true;

        });

        // 上一周
        $("#preWeekBtn").click(function () {
            var monDate = $("#monday").val();
            var preMon = dateToStr(addDate(monDate, -7));
            var preSun = dateToStr(addDate(monDate, -1));
            $("#monday").val(preMon);
            $("#sunday").val(preSun);
            $("#test6").val(preMon + ' - ' + preSun);
            projectTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    projectId: $("#searchProject").val(),
                    startDate: preMon,
                    endDate: preSun,
                    reviewFlag: "review"
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
        });

        // 下一周
        $("#nextWeekBtn").click(function () {
            var monDate = $("#monday").val();
            var nextMon = dateToStr(addDate(monDate, 7));
            var nextSun = dateToStr(addDate(monDate, 13));
            $("#monday").val(nextMon);
            $("#sunday").val(nextSun);
            $("#test6").val(nextMon + ' - ' + nextSun);
            projectTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    projectId: $("#searchProject").val(),
                    startDate: nextMon,
                    endDate: nextSun,
                    reviewFlag: "review"
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
        });

        // 本周一周
        $("#thisWeekBtn").click(function () {
            var now = new Date();
            // 获取周一和周末
            var start = getTime(0, now);
            var end = getTime(-6, now);

            $("#monday").val(start);
            $("#sunday").val(end);
            $("#test6").val(start + ' - ' + end);
            projectTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    projectId: $("#searchProject").val(),
                    startDate: start,
                    endDate: end,
                    reviewFlag: "review"
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
        });
    })
    ;
</script>

</body>
</html>